package com.yanqu.road.server.manger.activity.memorypieces;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.memorypieces.MemoryPiecesModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 记忆碎片
 */
public class MemoryPiecesMgr extends TempMgr {

    public static ActivityInfo activityInfo;

    /**
     * 记忆碎片道具id
     */
    public static int ZHOUNIAN_PUZZLE_ITEM_ID;

    /**
     * 解锁拼图消耗记忆碎片，格式：第1张图第1块拼图消耗碎片数量;第1张图第2个块拼图消耗碎片数量…|第2张图第1个块拼图消耗碎片数量;第2张图第2个块拼图消耗碎片数量…
     */
    public static String ZHOUNIAN_PUZZLE_ITEM_COST;

    /**
     * 消耗的记忆碎片list
     */
    public static List<List<Long>> puzzleItemCostList = new ArrayList<>();

    public static List<ActivityConditionInfo> memoryPiecesConditionInfoList = new ArrayList<>();

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        return super.save();
    }

    public static void reloadActivityData() {
        getLogger().info("reload MemoryPieces activity start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.MemoryPieces.getValue());
        if (openActivityInfoList.size() <= 0) {
            activityInfo = null;
            return;
        }
        activityInfo = openActivityInfoList.get(0);

        // 加载配置
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        ZHOUNIAN_PUZZLE_ITEM_ID = activityConfigMap.get(eActivityConfigType.ZHOUNIAN_PUZZLE_ITEM_ID.getName()).getIntValue();
        ZHOUNIAN_PUZZLE_ITEM_COST = activityConfigMap.get(eActivityConfigType.ZHOUNIAN_PUZZLE_ITEM_COST.getName()).getValue();
        String[] pieces = ZHOUNIAN_PUZZLE_ITEM_COST.split("\\|");
        List<List<Long>> list = new ArrayList<>();
        for (String piece : pieces) {
            List<Long> temp = new ArrayList<>();
            String[] pieceItem = piece.split(";");
            for (String s : pieceItem) {
                temp.add(Long.parseLong(s));
            }
            list.add(temp);
        }
        puzzleItemCostList = list;

        Map<Integer, List<ActivityConditionInfo>> tempActivityConditionInfoMap = new ConcurrentHashMap<>();
        Map<Integer, Map<Integer, List<ActivityConditionInfo>>> tempActivityConditionTypeInfoMap = new ConcurrentHashMap<>();
        ActivityBussiness.getActivityConditionInfoMap(tempActivityConditionTypeInfoMap, tempActivityConditionInfoMap, Collections.singletonList(activityInfo.getActivityId()));
        for (List<ActivityConditionInfo> conditionInfoList : tempActivityConditionInfoMap.values()) {
            memoryPiecesConditionInfoList.addAll(conditionInfoList);
        }

        // 在线玩家操作
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(MemoryPiecesModule.class).initSystem();
        }

        getLogger().info("reload MemoryPieces activity end");
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static ActivityInfo getInTimeActivityInfo() {
        return ActivityMgr.activityInTime(activityInfo) ? activityInfo : null;
    }

}
